package com.elric.mall.service.impl;

import cn.hutool.core.util.IdUtil;
import com.elric.mall.common.exception.ApiException;
import com.elric.mall.common.utils.ImportExcelUtil;
import com.elric.mall.dto.ZgBaseInfoImportDto;
import com.elric.mall.enums.ImportType;
import com.elric.mall.mapper.ZgBaseInfoMapper;
import com.elric.mall.model.ZgBaseInfo;
import com.elric.mall.model.ZgBaseInfoExample;
import com.elric.mall.service.ZgBaseInfoService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 实现功能：战规-基本信息实现层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 11:21
 */
@Service
public class ZgBaseInfoServiceImpl implements ZgBaseInfoService {
    @Autowired
    private ZgBaseInfoMapper zgBaseInfoMapper;

    @Override
    public int create(ZgBaseInfo zgBaseInfo) {
        zgBaseInfo.setId(IdUtil.fastUUID());
        return zgBaseInfoMapper.insert(zgBaseInfo);
    }

    @Override
    public int update(ZgBaseInfo zgBaseInfo) {
        return zgBaseInfoMapper.updateByPrimaryKeySelective(zgBaseInfo);
    }

    @Override
    public ZgBaseInfo findById(String id) {
        return zgBaseInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(String id) {
        return zgBaseInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ZgBaseInfo> list(Integer pageSize, Integer pageNum, ZgBaseInfo zgBaseInfo) {
        PageHelper.startPage(pageNum, pageSize);
        ZgBaseInfoExample example = new ZgBaseInfoExample();
        if(!StringUtils.isEmpty(zgBaseInfo.getText())) {
            example.createCriteria().andTextLike("%"+zgBaseInfo.getText()+ "%");
        }
        if(!StringUtils.isEmpty(zgBaseInfo.getType())) {
            example.createCriteria().andTypeEqualTo(zgBaseInfo.getType());
        }
        return zgBaseInfoMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<ZgBaseInfoImportDto> importDocuments(MultipartHttpServletRequest request) {
        return ImportExcelUtil.readExcelByRequest(request, new ZgBaseInfoImportDto());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBatch(List<ZgBaseInfo> list, ImportType importType) {
        if (importType == ImportType.full) {
            ZgBaseInfoExample example = new ZgBaseInfoExample();
            zgBaseInfoMapper.deleteByExample(example);
        }
        list.stream().forEach(l -> {
            l.setId(IdUtil.fastUUID());
        });
        return zgBaseInfoMapper.insertBatch(list);
    }
}
